/***********************************************************
 *
 * Gaoth
 * Copyright (c) 2011- Marcus Lundgren
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it freely,
 * subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such,
 *     and must not be misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 *
 ***********************************************************/

#include "game_window.h"

GameWindow::GameWindow( Model *model, NetworkManager *net_man )
: _model( model )
{
    // Initialize the window
    _device = irr::createDevice( irr::video::EDT_SOFTWARE, irr::core::dimension2d<irr::u32>(1024, 600), 16, false, false, true, 0);
    _device->setWindowCaption( L"Gaoth - Client" );

    _driver = _device->getVideoDriver();
    _scene_mgr = _device->getSceneManager();
    _gui_env = _device->getGUIEnvironment();

    // Initialize the states
    _about_state = new AboutState( _device, _driver, _scene_mgr, _gui_env, _model );
    _game_state = new GameState( _device, _driver, _scene_mgr, _gui_env, _model, net_man );
    _menu_state = new MenuState( _device, _driver, _scene_mgr, _gui_env, _model );

    _current_state = _menu_state;
    _current_state->init();
}

GameWindow::~GameWindow()
{
    // These are just pointers belonging to _window
    _driver    = 0;
    _scene_mgr = 0;
    _gui_env   = 0;

    _current_state = 0;

    // We are not the owner of model
    _model = 0;

    delete _device;

    delete _about_state;
    delete _game_state;
    delete _menu_state;
}

bool GameWindow::update( const float time )
{
    // The window has been closed
    if( !_device->run() )
        return false;

    // Check if we should switch to another state
    if( _current_state->is_done() )
    {
        const States next_state = _current_state->get_next_state();
        if( next_state == END_STATE )
            return false;
        else
            switch_state( next_state );
    }

    _current_state->update( time );

    return true;
}

void GameWindow::draw() const
{
    if( !_device->isWindowMinimized() )
        _current_state->draw();
}

void GameWindow::switch_state( const States state )
{
    // Switch to the wanted state, otherwise null it
    switch( state )
    {
        case ABOUT_STATE:
            _current_state = _about_state;
            break;
        case GAME_STATE:
            _current_state = _game_state;
            break;
        case MENU_STATE:
        default:
            _current_state = _menu_state;
            break;

    }

    // Init the state
    _current_state->init();
}
